package com.gbx.himalayating;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.gbx.himalayating.databinding.ActivityMainBinding;
import com.gbx.himalayating.entities.DiscoveryColumnEntity;
import com.gbx.himalayating.entities.FocusImages;
import com.gbx.himalayating.entities.HomeEntity;
import com.gbx.himalayating.entities.HotRecommendEntity;
import com.gbx.himalayating.entities.RecommendEntity;
import com.gbx.himalayating.utils.CommonRecyclerAdapter;
import com.gbx.himalayating.utils.NetService;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * shape 画小圆点
 */
public class MainActivity extends AppCompatActivity implements Callback<HomeEntity> {

    private ActivityMainBinding binding;
    private CommonRecyclerAdapter adapter;
    private NetService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        HashMap<Class<?>, Pair<Integer, Integer>> map = new HashMap<>();
        map.put(FocusImages.class,Pair.create(R.layout.focus_images, com.gbx.himalayating.BR.images));//单布局时，class为null
        map.put(DiscoveryColumnEntity.class,Pair.create(R.layout.discovery_item, com.gbx.himalayating.BR.column));
        map.put(RecommendEntity.class,Pair.create(R.layout.recommend_item, com.gbx.himalayating.BR.recommend));
        adapter = new CommonRecyclerAdapter(this, new ArrayList(), map);
        binding.recycler.setAdapter(adapter);
        service = new Retrofit.Builder()
                .baseUrl("http://www.baidu.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(NetService.class);
        service.getHome().enqueue(this);
        service.getHot().enqueue(this);
    }

    @Override
    public void onResponse(Call<HomeEntity> call, Response<HomeEntity> response) {
        HomeEntity body = response.body();
        FocusImages images = body.getFocusImages();
        adapter.add(0, images);
        adapter.add(1,body.getDiscoveryColumns());
        adapter.add(2,body.getEditorRecommendAlbums());
        HotRecommendEntity hotRecommends = body.getHotRecommends();
        if (hotRecommends != null) {
            adapter.addAll(hotRecommends.getList());
        }
    }

    @Override
    public void onFailure(Call<HomeEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();

    }
}
